@extends('backstage.layout.master')
@section('title', '订货单列表')
@section('style')
    <style>
        a{
            cursor: pointer;
        }
        th{
            text-align: center;
        }
    </style>
@stop
@section('content')
    <div class="breadcrumbs" id="breadcrumbs">
        <script type="text/javascript">
            try {
                ace.settings.check('breadcrumbs', 'fixed')
            } catch (e) {
            }
        </script>

        <ul class="breadcrumb">
            <li>
                <i class="icon-home home-icon"></i>
                <a target="_blank" href="backstage/home">主页</a>
            </li>

            <li class="active">
                <a target="_blank" href="/backstage/product-temp/product-temp-list">订货单</a>
            </li>
            <li>
                订货单列表
            </li>
        </ul><!-- .breadcrumb -->

        <div class="nav-search" id="nav-search">
        </div><!-- #nav-search -->
    </div>

    <div class="page-content">
        <div class="page-header">
            <h1>
                订货单
                <small>
                    <i class="icon-double-angle-right"></i>
                    订货单列表
                </small>
                <span class="pull-right">
                    <button class="btn btn-primary" onclick="javascript:window.location.href='{{asset('')}}backstage/purchase-order/add-purchase-order';">添加订货单</button>
                </span>
            </h1>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <div class="well">
                    <form action="javascript:void(0)" name="searchForm">
                        <img src="images/icon_search.gif" width="26" height="22" border="0" alt="SEARCH">
                        订货单状态:
                            <select id="sel_purchase_order_status">
                                <option @if($purchase_order_status==0) selected @endif value =0 >全部</option>
                                <option @if($purchase_order_status==1) selected @endif value =1 >未下单</option>
                                <option @if($purchase_order_status==2) selected @endif value =2 >已下单</option>
                                <option @if($purchase_order_status==3) selected @endif value =3 >在途</option>
                                <option @if($purchase_order_status==4) selected @endif value =4 >已入库</option>
                            </select>
                        供应商:
                        <select id="sel_supplier">
                            <option @if($supplier_id==0) selected @endif value =0 >全部</option>
                            @foreach($suppliers as $supplier)
                                <option @if(isset($supplier->supplier_id)&&($supplier_id==$supplier->supplier_id)) selected @endif value ="{{isset($supplier->supplier_id)?$supplier->supplier_id:0}}" >{{isset($supplier->supplier_name)?$supplier->supplier_name:''}}</option>
                            @endforeach
                        </select>
                        <input type="button" id="filter_btn" value="筛选" class="button" style="color: #0a03ff">
                    </form>
                </div>
            </div>
            <div class="col-xs-12">
                <!-- PAGE CONTENT BEGINS -->
                <div class="table-responsive">
                    <table id="sample-table-1" class="table table-striped table-bordered table-hover">
                        <thead>
                        <tr>
                            <th class="center">
                                <label>
                                    <input type="checkbox" class="ace">
                                    <span class="lbl"></span>
                                </label>
                            </th>
                            <th>Id</th>
                            <th>供应商</th>
                            <th>实际支付金额</th>
                            <th>运费</th>
                            <th>状态</th>
                            <th>更新时间</th>
                            <th>操作</th>
                        </tr>
                        </thead>

                        <tbody>
                        @foreach ($purchase_orders as $purchase_order)
                        <tr class="" data-tr-id={{isset($purchase_order->purchase_order_id)?$purchase_order->purchase_order_id:0}}>
                            <td class="center">
                                <label>
                                    <input type="checkbox" class="ace">
                                    <span class="lbl"></span>
                                </label>
                            </td>
                            <td>
                                {{isset($purchase_order->purchase_order_id)?$purchase_order->purchase_order_id:0}}
                            </td>
                            <td>
                                <a target="_blank" href="/backstage/supplier/detail?supplier_id={{isset($purchase_order->supplier_id)?$purchase_order->supplier_id:0}}">{{isset($purchase_order->supplier_name)?str_limit($purchase_order->supplier_name, $limit = 12, $end = '...'):''}}</a>
                            </td>
                            <td>
                                {{isset($purchase_order->actual_fee)?$purchase_order->actual_fee:''}}
                            </td>
                            <td>
                                {{isset($purchase_order->postage)?$purchase_order->postage:''}}
                            </td>
                            <td>
                                @if(($purchase_order->purchase_order_status!=1)&&($purchase_order->purchase_order_status!=2)&&($purchase_order->purchase_order_status!=3)&&($purchase_order->purchase_order_status!=4))
                                    未知错误
                                @elseif($purchase_order->purchase_order_status==4)
                                    已入库
                                @else
                                <select id="purchase_order_status_{{$purchase_order->purchase_order_id}}" onchange="changePurchaseOrderStatus('{{$purchase_order->purchase_order_id}}')">
                                 <option @if($purchase_order->purchase_order_status==1) selected @endif value =1 >未下单</option>
                                 <option @if($purchase_order->purchase_order_status==2) selected @endif value =2 >已下单</option>
                                 <option @if($purchase_order->purchase_order_status==3) selected @endif value =3 >在途</option>
                                 <option @if($purchase_order->purchase_order_status==4) selected @endif value =4 >已入库</option>
                                </select>
                                @endif
                            </td>
                            <td>
                                {{isset($purchase_order->updated_at)?date('Y-m-d H:i:s',$purchase_order->updated_at):''}}
                            </td>
                            <td class="center">
                                <div class="visible-md visible-lg hidden-sm hidden-xs action-buttons">

                                    @if(isset($purchase_order->purchase_order_status)&&($purchase_order->purchase_order_status==1))
                                     <a class="detail" target="_blank"
                                       onclick="javascript:window.location.href='/backstage/purchase-order/update-purchase-order?id={{isset($purchase_order->purchase_order_id)?$purchase_order->purchase_order_id:0}}';"
                                       title="编辑" style="color:#9e00ff">
                                        编辑
                                     </a>
                                    @else
                                      <a class="detail" target="_blank"
                                           onclick="javascript:window.location.href='/backstage/purchase-order/purchase-order-detail?id={{isset($purchase_order->purchase_order_id)?$purchase_order->purchase_order_id:0}}';"
                                           title="查看详情" style="color:#2709ff">
                                            查看详情
                                      </a>
                                    @endif
                                    <a  data-delete="{{isset($purchase_order->purchase_order_id)?$purchase_order->purchase_order_id:0}}" style = "cursor:pointer;color: red;margin-left: 15px">
                                        <span style="font-weight: bold">删除</span>
                                    </a>
                                </div>
                            </td>
                        </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
                <div class="row">
                    <div class="col-sm-6">
                        <div class="dataTables_info" id="sample-table-2_info"></div>
                    </div>
                    <div class="col-sm-6">
                        <div class="dataTables_paginate paging_bootstrap">
                            <?php echo $purchase_orders->render(); ?>
                        </div>
                    </div>
                </div>
                <!-- PAGE CONTENT ENDS -->
        </div><!-- /.row -->
    </div><!-- /.page-content -->
@stop
@section('script')
    <script type="text/javascript">
        $(function () {
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            $('[data-rel=tooltip]').tooltip();
            $('[data-delete]').click(function(){
                var  id = $(this).attr("data-delete");
                if(id!==0){
                    swal({
                        title: "确定删除？？",
                        text: "",
                        type: "warning",
                        showCancelButton: true,
                        confirmButtonColor: "#DD6B55",
                        confirmButtonText: "确定",
                        cancelButtonText: "取消",
                        closeOnConfirm: true,
                        closeOnCancel: true
                    },function (){
                        $.ajax({
                            type: "POST",
                            url: "/backstage/purchase-order/del-purchase-order",
                            data: {'id':id},
                            success: function (msg) {
                                if(msg.state==1){
                                    swal({title: "删除成功！", text: "",type: "success", timer:1000 })
                                    $('[data-tr-id='+id+']').remove();
                                }else {
                                    swal({title: msg.message, text: "",type: "error", timer:1000 })
                                }

                            }, error: function () {
                                swal({title: "系统繁忙！", text: "",type: "error", timer:1000 })
                            }
                        });
                    });
                }
            });
            $('#filter_btn').click(function() {
                var status=$('#sel_purchase_order_status option:selected').val();
                var supplier_id=$('#sel_supplier option:selected').val();
                window.location.href="/backstage/purchase-order/purchase-order-list?status="+status+'&supplier_id='+supplier_id;
            });
        });
        function changePurchaseOrderStatus(id) {
            var purchase_order_status_id='purchase_order_status_'+id;
            var purchase_order_status=$('#'+purchase_order_status_id+' option:selected').val();
            if(purchase_order_status==0){
                swal({title: "状态未知错误！", text: "",type: "error", timer:500 });
                return;
            }
            $.ajax({
                type: "POST",
                cache: true,
                url: "/backstage/purchase-order/update-status",
                data:{'id':id,'purchase_order_status':purchase_order_status},
                success: function (msg) {
                    if(msg.state==1){
                        swal({title: "更新成功！", text: "",type: "success", timer:1000 });
                        location.reload();
                    }else {
                        swal({title: msg.message, text: "",type: "error", timer:1000 });
                    }
                }, error: function () {
                    swal({title: "系统繁忙！", text: "",type: "error", timer:1000 });
                }
            });
        }
    </script>
@stop